<?php
namespace App\Model;
use App\Model\ClassTable;
use App\Repository\Sys\Db;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//管理员表
class AdminTable{
    use Db;
    static private $table = 'admin';
    static public function getAdminList($page,$limit,$title){
    	$offset = ($page - 1) * $limit;
        //拼接sql
        $where = "d.id > 1";
        $parameters = array(
            'limit' => $limit,
            'offset' => $offset
        );
        if ($title) {
            $where .= " AND (d.username LIKE :username or d.phone LIKE :phone )";
            $parameters['name'] = "%{$title}%";
            $parameters['phone'] = "%{$title}%";
        }
        $sql = "select * from admin as d  WHERE {$where} ORDER BY d.id DESC limit  :offset,:limit;";
        //执行sql
        $res = \DB::select($sql, $parameters);
        unset($parameters['limit'], $parameters['offset']);
        $count = \DB::select("select count(*) as c from admin as d WHERE {$where}", $parameters)[0]->c;
        $data=[];
        foreach ($res as $v) {
        	$v->created_at = Str::dateFormat('Y-m-d', $v->created_at);
        	$v->status_text = ConstTable::STATUS_NAME[$v->status];
        	if($v->id_join){
        		$join_info=ClassTable::getClassInfo($v->id_join)['data'];
        		$v->join_text = $join_info->name;
        	}
        	$data[] = $v;
        }
        return JsonRespose::jsonArray(1,[
            'count' => $count,
            'limit' => $limit,
            'page' => $page,
            'list' => $data
        ]);
    }
    //管理员-详情
    static public function getAdminInfo($id){
    	$res = AdminTable::ORM()->where('id', $id)->first();
        if (!$res) {
            return JsonRespose::fail('数据不存在');
        }
        $data = $res;
        $data->pwd='';
        return JsonRespose::jsonArray(1,$data);
    }
}